package com.leetcode;

/**
 * Created by Z-hong on 2023/2/28.
 */

public class LeetCode5 {
    public static String longestPalindrome1(String s) {
        String res = "";
        StringBuffer sb=new StringBuffer(s);
        for (int i = 0; i <sb.length(); i++) {
            for (int j = i+1; j <= sb.length(); j++) {
                String b=sb.substring(i, j);
                StringBuffer a =new StringBuffer(b);
                a.reverse();
                if (b.equals(a.toString())&&a.length()>res.length()){
                    res=a.toString();
                }
            }
        }
        return res;
    }

    public static String longestPalindrome(String s) {
        StringBuffer sb=new StringBuffer(s);
        int length=s.length();
        int i=0;
        while(length>1){
            for (int k = 0; k <=s.length()-length; k++) {
                if (sb.charAt(k)!=sb.charAt(k+length-1))continue;
                String b=sb.substring(k, k+length);
                StringBuffer a =new StringBuffer(b);
                a.reverse();
                if (b.equals(a.toString())){return b;}
            }
            length--;
        }
        char c = sb.charAt(0);
        return String.valueOf(c);
    }


    public static void main(String[] args) {
        System.out.println(longestPalindrome("cbbd"));
    }
}
